/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import { Button, message } from 'antd';
import ProForm, { ProFormDatePicker, ProFormSelect } from '@ant-design/pro-form';
import type { ProFormInstance } from '@ant-design/pro-form';
import type { RangePickerProps } from 'antd/es/date-picker';
import { makeExcel } from '@/services/Finance/jdkDeliveryTransfer';
import { getBrandList } from '@/services/jdk/shield';
import moment from 'moment';
import { FormColFormat, delivery_type } from '../_utils/index.config';

const Index = (props: { onChange: Function }) => {
  const { onChange } = props;
  const formRef = useRef<ProFormInstance>();

  const handleSubmit = async (values: any) => {
    makeExcel(values).then((res) => {
      const { code, msg } = res;
      if (code === 0) {
        formRef.current?.resetFields();
        message.success('操作成功');
        onChange('list');
      } else {
        message.error(msg);
      }
    });
  };

  const disabledDate: RangePickerProps['disabledDate'] = (current) => {
    return current < moment().month(2) || (current && current > moment().endOf('day'));
  };
  return (
    <ProForm
      formRef={formRef}
      layout='horizontal'
      onFinish={handleSubmit}
      {...FormColFormat}
      initialValues={{
        delivery_brand: 'gxd',
        delivery_type: '0',
      }}
      submitter={{
        render: (form) => [
          <Button style={{ opacity: '0' }}>重置</Button>,
          <Button type='primary' onClick={form.submit}>
            导出对账单
          </Button>,
        ],
      }}
    >
      <ProFormDatePicker
        name='date'
        label='下单日期'
        rules={[{ required: true, message: '请选择下单日期' }]}
        fieldProps={{
          picker: 'month',
          disabledDate: disabledDate,
          placeholder: '请选择日期',
          format: 'YYYY-MM',
        }}
      />
      <ProFormSelect
        name='delivery_type'
        label='平台运力'
        rules={[{ required: true, message: '请选择运力平台' }]}
        valueEnum={delivery_type}
      />
      <ProFormSelect
        name='delivery_brand'
        label='运力品牌'
        rules={[{ required: true, message: '请选择运力品牌' }]}
        request={getBrandList}
        placeholder='请选择运力品牌'
      />
    </ProForm>
  );
};

export default Index;
